/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.chartextensions.averageloader;

import JFreeChartExtensions.chartextensions.IChartExtensionManager;
import JFreeChartExtensions.chartextensions.utils.AverageCalculator;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.listeners.IChartMouseActionsListener;
import JFreeChartExtensions.listeners.IChartOnPaintListener;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import org.jfree.chart.ChartPanel;

/**
 *
 * @author Nobody
 */
public class AverageLoaderManager implements IChartExtensionManager, IChartMouseActionsListener, IChartOnPaintListener {

    boolean draw = true;
    boolean dragged = false;
    private int lastPressedButton;
    private boolean isNewRect = true;
    private ExtendableChartPanel chartPanel;
    private AverageCalculator averageCalculator;
  /*  private ProgressDialog progressDialog;

    public ProgressDialog getProgressDialog() {
        return progressDialog;
    }*/

    @Override
    public void register(ChartPanel chartPanel) {
        this.chartPanel = (ExtendableChartPanel) chartPanel;
        averageCalculator = new AverageCalculator((ExtendableChartPanel) chartPanel, this);
        ExtendableChartPanel extendableChartPanel = (ExtendableChartPanel) chartPanel;
        extendableChartPanel.addChartMouseActionListener(this);
        extendableChartPanel.addChartOnPaintListener(this);
    }

    @Override
    public void unregister() {
        chartPanel.removeChartMouseActionListener(this);
        chartPanel.removeChartOnPaintListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent event) {
    }

    @Override
    public void mousePressed(MouseEvent event) {
        lastPressedButton = event.getButton();
        if (event.getButton() == MouseEvent.BUTTON1) {
            chartPanel.setDomainZoomable(false);
            chartPanel.setRangeZoomable(false);
            averageCalculator.setX1(event.getX());
            averageCalculator.setY1(event.getY());
            this.isNewRect = true;
            draw = true;
        } else if (event.getButton() == MouseEvent.BUTTON3) {
            draw = true;
        } else {
            chartPanel.setDomainZoomable(true);
            chartPanel.setRangeZoomable(true);
            draw = false;
        }
    }

    @Override
    public void mouseReleased(MouseEvent event) {
        if (event.getButton() == MouseEvent.BUTTON1) {
            averageCalculator.setX2(event.getX());
            averageCalculator.setY2(event.getY());
        }
        if (dragged && lastPressedButton == MouseEvent.BUTTON1) {
            averageCalculator.showGUI();
            dragged = false;
        }
    }

    public void setDraw(boolean draw) {
        this.draw = draw;
    }

    @Override
    public void mouseDragged(MouseEvent event) {
        averageCalculator.setX2(event.getX());
        averageCalculator.setY2(event.getY());
        this.isNewRect = false;
        this.dragged = true;

    }

    @Override
    public void chartOnPaint(Graphics g) {
        int width = averageCalculator.getWidth();
        int height = averageCalculator.getHeight();
        int w = Math.abs(width);
        int h = Math.abs(height);
        int x = width < 0 ? averageCalculator.getX1() : averageCalculator.getX2();
        int y = height < 0 ? averageCalculator.getY1() : averageCalculator.getY2();

        if (!this.isNewRect && draw && lastPressedButton != MouseEvent.BUTTON3) {
            g.drawRect(x, y, w, h);
        }
     //   if(progressDialog != null && progressDialog.isVisible() && progressDialog.getProgress().getModel().getMaximum() == 100){
      //      progressDialog.dispose();
      //  }
    }

   /* public void setProgressDialog(ProgressDialog progressDialog) {
        this.progressDialog = progressDialog;
    }*/

}
